package com.myqf.d3_多线程.线程创建方式;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class FutureTask创建方式 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        FutureTask futureTask = new FutureTask(new Callable() {
            @Override
            public Object call() throws Exception {
                for (int i = 0; i < 10; i++) {
                    System.out.println(i);
                    Thread.sleep(100);
                }
                return "a";
            }
        });

        Thread t1 = new Thread(futureTask,"f1");
        t1.start();


        System.err.println("线程是否被取消？" + futureTask.isCancelled());
        System.err.println("线程是已完成？" + futureTask.isDone());
//      futureTask.cancel(true);// 取消线程
        System.out.println("线程运行完毕之后的返回值是：" + futureTask.get());// 此方法会等futureTask线程执行完毕才运行，造成阻塞






    }
}

